courses={
	1:'小学语文',
	2:'小学数学',
	3:'小学英语',
	4:'中学语文',
	5:'中学数学',
	6:'中学英语',
	7:'中学生物',
	8:'中学化学'
};

function courseWindow(opts){
	this.window = new at_trace();
	this.init(opts);
}
courseWindow.prototype = (function(){
	return {
		init:function(opts){
			var items = opts.items || courses;
			var html='';
			for(k in items){
				html+='<input type="radio" name="course_win" value="'+k+'" id="course_'+k+'" /><label for="course_'+k+'">'+items[k]+'</label>';
			}
			var w = this.window;
			w.setWidth(500);
			w.setContact('课程列表',html,[this.window.getButton("确定", "yellow", function() {
				var c = $('#at_trace_w'+w.randomId).find('input[name="course_win"]:checked');
				if(c.length == 0) {
					at_infoTrace.show('请选择一个课程', "e", null, null, true);
				} else {
					var v = c.val();
					if(opts.callback){
						opts.callback({key:v, value:items[v]});
					}
					w.setDisplay('hide');
				}
				return false;
			})]);
		},
		show:function(){
			this.window.setDisplay('show');
		},
		hide:function(){
			this.window.setDisplay('hide');
		}
	};
})();
function tutorForm(options){
	var id=options.id ||'tutorForm';
	id='#'+id;
	var form = $(id);
	this.form=form;
	this.title = form.find('#title');
	this.subject = form.find('#subject');
	this.subjectV = form.find('#subjectV');
	this.subjectChoose = form.find('#choose');
	this.courseType=form.find('input[name="courseType"]');
	this.courseNum=form.find('#courseNum');
	this.feeType=form.find('input[name="feeType"]');
	this.chargeType=form.find('input[name="chargeType"]');
	this.chargeValue=form.find('#chargeValue');
	this.teachWay=form.find('input[name="teachWay"]');
	this.memo=form.find('#memo');
	this.init(options);
}
tutorForm.prototype=(function(){
	function isNum(v){
		return /^(-|)\d+(\.d+)?$/.test(v);
	}
	function applyValidation(form, opts){
		
	};
	function applyTips(tutorForm){
		var f = tutorForm;
		//标题
		f.title.focus(function(){
			changeState(f.title, "insert", f.msgs.title);
		}).blur(function(){
			if(f.title.val() == ''){
				changeState(f.title, "error",f.msgs.title);
				return;
			} else {
				changeState(f.title, "ok", f.msgs.empty);
			}
		});
		f.chargeValue.focus(function(){
			changeState(f.chargeValue, "insert", f.msgs.chargeValue);
		}).blur(function(){
			var v = f.chargeValue.val();
			if(v == ''){
				changeState(f.chargeValue, "error",f.msgs.chargeValue);
				return;
			} else {
				if(!isNum(v)) {
					changeState(f.chargeValue, "error", v+'不是数字类型，'+f.msgs.notNum);
					f.chargeValue.val('');
				} else {
					if(v <= 0) {
						changeState(f.chargeValue, "error", f.msgs.lessZero);
					} else {
						changeState(f.chargeValue, "ok", f.msgs.empty);
					}
				}
			}
		});
		f.courseNum.focus(function(){
			changeState(f.courseNum, "insert", f.msgs.courseNum);
		}).blur(function(){
			var v = f.courseNum.val();
			if(v == ''){
				changeState(f.courseNum, "error",f.msgs.courseNum);
				return;
			} else {
				if(!isNum(v)) {
					changeState(f.courseNum, "error", v+'不是数字类型，'+f.msgs.notNum);
					f.courseNum.val('');
				} else {
					if(v <= 0) {
						changeState(f.courseNum, "error", f.msgs.lessZero);
					} else {
						changeState(f.courseNum, "ok", f.msgs.empty);
					}
				}
			}
		});
	};
	function bindEvent(tutorForm) {
		var f = tutorForm;
		f.courseType.click(function(){
			if(this.value==0){
				f.courseNum.parent().parent().parent().hide();
			} else {
				f.courseNum.parent().parent().parent().show();
			}
		});
		f.feeType.click(function(){
			if(this.value==0){
				f.chargeType.parent().parent().parent().hide();
				f.chargeValue.parent().parent().parent().hide();
			} else {
				f.chargeType.parent().parent().parent().show();
				f.chargeValue.parent().parent().parent().show();
			}
		});
		f.subjectChoose.click(function(){
			var w = f.hasSubjectW;
			if(w){
				w.show();
			} else {
				w = f.hasSubjectW = new courseWindow({items:courses, callback:function(re){
					f.subject.val(re.key);
					f.subjectV.val(re.value);
				}});
				w.show();
			}
			
		});
	}
	function changeState(item, method, readme){
		if(item.length<1 || method!="insert" && method!="default" && method!="ok" && method!="error") return;
		var span = item.parent();
		span.find('em').remove();
		if(method=="insert") mode = "i";
		if(method=="default") mode = "d";
		if(method=="ok") mode="o";
		if(method=="error") mode="e";
		if(/i|o|e/i.test(mode)){
			span.append("<em><u class=\"at_msg m_3_"+mode+"\"></u>"+readme+"</em>");
		}
		span.addClass("t_1_"+mode);
	};
	return {
		init : function(opts){
			this.courseNum.parent().parent().parent().hide();
			this.chargeType.parent().parent().parent().hide();
			this.chargeValue.parent().parent().parent().hide();
			this.msgs={
				empty:'',
				title:'标题应为<span class=\"orange1\">2</span>~<span class=\"orange1\">50</span>个字',
				subject:'标题应为<span class=\"orange1\">2</span>~<span class=\"orange1\">50</span>个字',
				chargeValue:'请输入费用信息，金额单位为人民币RMB',
				courseNum:'请输入上限人数',
				notNum:'请输入数字类型',
				lessZero:'输入不能小于0'
			};
			applyTips(this);
			bindEvent(this);
		}
		
	};
})();